package com.jiandian.controller;

import com.jiandian.configration.OssConfig;
import com.jiandian.crudparams.dto.AddBannerDto;
import com.jiandian.crudparams.dto.EditBannerDto;
import com.jiandian.crudparams.swaggernotes.BannerNotes;
import com.jiandian.entity.Banner;
import com.jiandian.entity.Dict;
import com.jiandian.page.PageInfo;
import com.jiandian.service.BannerService;
import com.jiandian.util.BaseController;
import com.jiandian.util.BaseResult;
import com.jiandian.util.BeanUtils;
import com.jiandian.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图片
 *
 * @author Houqi Fu
 * @date 2019/01/24
 */
@CrossOrigin
@Api(value = "Banner", description = "轮播图片")
@RestController
@RequestMapping(value = "${jiandian.api.version}/Banner")
public class BannerController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BannerController.class);

    @Autowired
    private BannerService service;

    @Autowired
    private OssConfig ossConfig;
    //
    // @ApiOperation(value = "添加", notes = BannerNotes.ADD_NOTE)
    // @ApiImplicitParam(name = "record", value = BannerNotes.ADD_VALUE, required = true, dataType = "AddBannerDto")
    // @PostMapping("")
    // public BaseResult add(@RequestBody @Validated AddBannerDto record, BindingResult result){
    //     logger.info("添加轮播图片，参数：\n {}", ReflectionToStringBuilder.toString(record));
    //     if (result.hasErrors()) {
    //         FieldError fieldError = result.getFieldError();
    //         logger.error("添加轮播图片参数错误:{}", fieldError.getDefaultMessage());
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
    //     }
    //     Banner bean = new Banner();
    //     BeanUtils.copyProperties(record, bean);
    //     Banner saveResult = service.add(bean);
    //     if (saveResult != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, saveResult);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }
    //
    // @ApiOperation(value = "修改", notes = BannerNotes.EDIT_NOTE)
    // @ApiImplicitParam(name = "record", value = BannerNotes.EDIT_VALUE, required = true, dataType = "EditBannerDto")
    // @PutMapping("/{id}")
    // public BaseResult edit(@PathVariable("id") String id, @RequestBody @Validated EditBannerDto record, BindingResult result){
    //     logger.info("修改轮播图片，参数：\n {}", ReflectionToStringBuilder.toString(record));
    //     if (result.hasErrors()) {
    //         FieldError fieldError = result.getFieldError();
    //         logger.error("修改轮播图片参数错误:{}", fieldError.getDefaultMessage());
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
    //     }
    //     Banner bean = new Banner();
    //     bean.setId(Integer.valueOf(id));
    //     BeanUtils.copyProperties(record, bean);
    //     Banner updateResult = service.update(bean);
    //     if (updateResult != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, updateResult);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }

    @ApiOperation(value = "列表", notes = BannerNotes.LIST_NOTE)
    @ApiImplicitParam(name = "pageInfo", value = BannerNotes.LIST_VALUE, required = true, dataType = "PageInfo")
    @GetMapping("")
    public BaseResult list(Banner whereEntity,PageInfo pageInfo){
        logger.info("查询轮播图片，参数：\n {}", ReflectionToStringBuilder.toString(pageInfo));
        PageInfo pageResult = service.getPage(whereEntity, pageInfo);
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,pageResult);
    }

    @ApiOperation(value = "新增的轮播图片")
    @GetMapping("/new")
    public BaseResult banner(){
        logger.info("查询轮播图片");
        Dict bannerNew = service.getBannerNew(ossConfig.getBucketName());
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,bannerNew.getValue());
    }

    @ApiOperation(value = "手动更新轮播图片")
    @GetMapping("/sync")
    public BaseResult sync(){
        logger.info("手动更新轮播图片");
        try {
            String urls = service.fetchBannerUrls(ossConfig.getBucketName());
            service.updateBanner(urls);
        }catch (Exception e){
            throw new RuntimeException("手动更新轮播图片失败!");
        }
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,"");
    }
    // @ApiOperation(value = "详情", notes = "轮播图片详情")
    // @GetMapping("/{id}")
    // public BaseResult detail(@PathVariable("id") String id){
    //     logger.info("查询轮播图片，参数：\n id = {}", id);
    //     if (StringUtils.isBlank(id)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数uuid不存在");
    //     }
    //     Banner record = service.findById(Integer.valueOf(id));
    //     if (record != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, record);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "不存在");
    // }
    //
    // @ApiOperation(value = "删除", notes = "根据轮播图片id删除")
    // @DeleteMapping("/{id}")
    // public BaseResult delete(@PathVariable("id") String id){
    //     logger.info("删除轮播图片，参数：\n id = {}", id);
    //     if (StringUtils.isBlank(id)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数id不存在");
    //     }
    //     int deleteResult = service.delete(Integer.valueOf(id));
    //     if (deleteResult > 0) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }
    //
    // @ApiOperation(value = "批量删除", notes = "根据轮播图片id list批量删除")
    // @PostMapping("batchDelete")
    // public BaseResult batchDelete(@RequestBody List<Integer> ids){
    //     logger.info("批量删除轮播图片，参数：\n {}", ReflectionToStringBuilder.toString(ids, ToStringStyle.MULTI_LINE_STYLE));
    //     if (CollectionUtils.isEmpty(ids)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数ids不存在");
    //     }
    //     int deleteResult = service.batchDeleteById(ids);
    //     if (deleteResult > 0) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }

}
